{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\".\\\\diyLogo.png\" alt=\"some_text\">\n",
    "<h1> 第三讲 程序设计基础</h1>\n",
    "<a id=backup></a>\n",
    "<H2>目录</H2>  \n",
    "\n",
    "[3.1 组合数据类型概述](#Section1)  \n",
    "[3.2 序列类型](#Section2)  \n",
    "[3.3 集合类型](#Section3)     \n",
    "[3.4 映射类型](#Section4)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id = Section1> </a>\n",
    "## 3.1 组合数据类型概述\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id = Section2> </a>\n",
    "## 3.2 序列类型\n",
    "### 字符串\n",
    "### 元组\n",
    "### 列表\n",
    "\n",
    "<p><a href=\"https://yanghailin.blog.csdn.net/article/details/81126087?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link\">\n",
    "this is example of python</a></p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.1字符串运算\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b 输出结果： HappyI Love You\n",
      "a * 2 输出结果： HappyHappy\n",
      "a[1] 输出结果： a\n",
      "a[1:4] 输出结果： app\n",
      "H 在变量 a 中\n",
      "M 不在变量 a 中\n",
      "\\n\n",
      "\\n\n"
     ]
    }
   ],
   "source": [
    "a = \"Happy\"\n",
    "b = \"I Love You\"\n",
    " \n",
    "print(\"a + b 输出结果：\", a + b)\n",
    "print(\"a * 2 输出结果：\", a * 2)\n",
    "print(\"a[1] 输出结果：\", a[1])\n",
    "print(\"a[1:4] 输出结果：\", a[1:4])\n",
    "# in \n",
    "if( \"H\" in a) :\n",
    "    print(\"H 在变量 a 中\")\n",
    "else :\n",
    "    print(\"H 不在变量 a 中\")\n",
    "#not in \n",
    "if( \"M\" not in a) :\n",
    "    print(\"M 不在变量 a 中\")\n",
    "else :\n",
    "    print(\"M 在变量 a 中\")\n",
    "# r R 直接打印字符串\n",
    "print (r'\\n')\n",
    "print (R'\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.2元组\n",
    "Python 的元组与列表类似，不同之处在于元组的元素不能修改,可以合并。\n",
    "\n",
    "元组使用小括号 ( )，列表使用方括号 [ ]。\n",
    "\n",
    "元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Google', 'Runoob', 1997, 2000) \n",
      " tup2 \n",
      " ('a', 'b', 'c', 'd') \n",
      " <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tup1 = ('Google', 'Runoob', 1997, 2000)\n",
    "tup2 = (1, 2, 3, 4, 5 )\n",
    "tup3 = \"a\", \"b\", \"c\", \"d\"   #  不需要括号也可以\n",
    "type(tup3)\n",
    "print(tup1,\"\\n\",\"tup2\",\"\\n\",tup3,\"\\n\",type(tup3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2448321434544\n",
      "2448321266176\n"
     ]
    }
   ],
   "source": [
    "print(id(tup1))\n",
    "tup1=1,2,3\n",
    "print(id(tup1))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.3列表\n",
    "序列是 Python 中最基本的数据结构。\n",
    "\n",
    "序列中的每个值都有对应的位置值，称之为索引，第一个索引是 0，第二个索引是 1，依此类推。\n",
    "\n",
    "Python 有 6 个序列的内置类型，但最常见的是列表和元组。\n",
    "\n",
    "列表都可以进行的操作包括索引，切片，加，乘，检查成员。\n",
    "\n",
    "此外，Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。\n",
    "\n",
    "列表是最常用的 Python 数据类型，它可以作为一个方括号内的逗号分隔值出现。\n",
    "\n",
    "列表的数据项不需要具有相同的类型\n",
    "\n",
    "创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yellow\n",
      "blue\n",
      "yellow\n"
     ]
    }
   ],
   "source": [
    "list = ['red', 'green', 'blue', 'yellow', 'white', 'black']\n",
    "print( list[3] )\n",
    "print( list[-4] )\n",
    "print( list[-3] )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list[0]:  Google\n",
      "list[0:4]:  ['Google', 'Runoob', 'Zhihu', 'Taobao', 'Wiki']\n"
     ]
    }
   ],
   "source": [
    "list = ['Google', 'Runoob', \"Zhihu\", \"Taobao\", \"Wiki\"]\n",
    " \n",
    "# 读取第二位\n",
    "print (\"list[0]: \", list[0])\n",
    "# 从第二位开始（包含）截取到倒数第二位（不包含）\n",
    "print (\"list[0:4]: \", list[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的第三个元素为 :  ['Google', 'Runoob', 2001, 'Taobao', 'Wiki']\n",
      "更新后的列表 :  ['Google', 'Runoob', 2001, 'Taobao', 'Wiki', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "list = ['Google', 'Runoob', \"Zhihu\", \"Taobao\", \"Wiki\"]\n",
    "list[2] = 2001\n",
    "print (\"更新后的第三个元素为 : \", list)\n",
    " \n",
    "list.append('Baidu')\n",
    "print (\"更新后的列表 : \", list)\n",
    "list[:]=''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除第三个元素 :  ['Google', 'Runoob', 'Taobao', 'Wiki']\n"
     ]
    }
   ],
   "source": [
    "#删除元素\n",
    "list = ['Google', 'Runoob', \"Zhihu\", \"Taobao\", \"Wiki\"]\n",
    "del list[2]\n",
    "print (\"删除第三个元素 : \", list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除第三个元素 :  ['A', 'B', 'D', 'E']\n"
     ]
    }
   ],
   "source": [
    "#删除元素\n",
    "list = ['A', 'B', \"C\", \"D\", \"E\"]\n",
    "del list[2]\n",
    "print (\"删除第三个元素 : \", list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python列表函数&方法\n",
    "Python包含以下函数:\n",
    "<table><tr>\n",
    "<th style=\"width:5%\">序号</th><th style=\"width:95%\">函数</th></tr>\n",
    "\n",
    "<tr><td>1</td><td><a href=\"python3-att-list-len.html\">len(list)</a><br>列表元素个数</td></tr>\n",
    "<tr><td>2</td><td><a href=\"python3-att-list-max.html\">max(list)</a><br>返回列表元素最大值</td></tr>\n",
    "<tr><td>3</td><td><a href=\"python3-att-list-min.html\">min(list)</a><br>返回列表元素最小值</td></tr>\n",
    "<tr><td>4</td><td><a href=\"python3-att-list-list.html\">list(seq)</a><br>将元组转换为列表</td></tr>\n",
    "</table>\n",
    "\n",
    "Python包含以下方法:\n",
    "<table><tr>\n",
    "<th style=\"width:5%\">序号</th><th style=\"width:95%\">方法</th></tr>\n",
    "<tr><td>1</td><td><a href=\"python3-att-list-append.html\">list.append(obj)</a><br>在列表末尾添加新的对象</td></tr>\n",
    "<tr><td>2</td><td><a href=\"python3-att-list-count.html\">list.count(obj)</a><br>统计某个元素在列表中出现的次数</td></tr>\n",
    "<tr><td>3</td><td><a href=\"python3-att-list-extend.html\">list.extend(seq)</a><br>在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td></tr>\n",
    "<tr><td>4</td><td><a href=\"python3-att-list-index.html\">list.index(obj)</a><br>从列表中找出某个值第一个匹配项的索引位置</td></tr>\n",
    "<tr><td>5</td><td><a href=\"python3-att-list-insert.html\">list.insert(index, obj)</a><br>将对象插入列表</td></tr>\n",
    "<tr><td>6</td><td><a href=\"python3-att-list-pop.html\">list.pop([index=-1])</a><br>移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td></tr>\n",
    "<tr><td>7</td><td><a href=\"python3-att-list-remove.html\">list.remove(obj)</a><br>移除列表中某个值的第一个匹配项</td></tr>\n",
    "<tr><td>8</td><td><a href=\"python3-att-list-reverse.html\">list.reverse()</a><br>反向列表中元素</td></tr>\n",
    "<tr><td>9</td><td><a href=\"python3-att-list-sort.html\">\tlist.sort( key=None, reverse=False)</a><br>对原列表进行排序</td></tr>\n",
    "<tr><td>10</td><td><a href=\"python3-att-list-clear.html\">list.clear()</a><br>清空列表</td></tr>\n",
    "<tr><td>11</td><td><a href=\"python3-att-list-copy.html\">list.copy()</a><br>复制列表</td></tr>\n",
    "</table>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mitsubishi', 'Ford', 'BMW', 'VW']\n"
     ]
    }
   ],
   "source": [
    "# A function that returns the length of the value:\n",
    "def myFunc(e):\n",
    "  return len(e)\n",
    "cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']\n",
    "cars.sort(reverse=True, key=myFunc)\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mitsubishi', 'Ford', 'BMW']\n"
     ]
    }
   ],
   "source": [
    "cars.pop()\n",
    "print(cars)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=Section3></a>\n",
    "## 3.3 集合类型\n",
    "集合（set）是一个无序的不重复元素序列。\n",
    "\n",
    "可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3.1基本操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(len(basket))                      # 这里演示的是去重功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basker = {'orange', 'banana', 'pear', 'apple'}\n",
    "'orange' in basket                 # 快速判断元素是否在集合内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basker = {'orange', 'banana', 'pear', 'apple'}\n",
    "'crabgrass' in basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= {'b', 'y', 'a', 'f', 'd', 'r', 'c', 't'}\n",
      "b= {'u', 'y', 'a', 'w', 'c', 'z', 'o', 'e', 'r', 'q', 'm', 'l', 'i', 't'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'f'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面展示两个集合间的运算.\n",
    "a = set('abracadabradtrftyytytytfyttrd')\n",
    "b = set('alacazamqwertyuio')\n",
    "print('a=',a)\n",
    "print('b=',b)\n",
    "# 集合a中包含而集合b中不包含的元素                                 \n",
    "a - b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a',\n",
       " 'b',\n",
       " 'c',\n",
       " 'd',\n",
       " 'e',\n",
       " 'f',\n",
       " 'i',\n",
       " 'l',\n",
       " 'm',\n",
       " 'o',\n",
       " 'q',\n",
       " 'r',\n",
       " 't',\n",
       " 'u',\n",
       " 'w',\n",
       " 'y',\n",
       " 'z'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合a或b中包含的所有元素（并集）\n",
    "a | b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'c', 'r', 't', 'y'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合a和b中都包含了的元素(交集)\n",
    "a & b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'b', 'd', 'e', 'f', 'i', 'l', 'm', 'o', 'q', 'u', 'w', 'z'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不同时包含于a和b的元素（交集的补集）\n",
    "a ^ b                              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'r', 'd'}\n"
     ]
    }
   ],
   "source": [
    "#类似列表推导式，同样集合支持集合推导式(Set comprehension):\n",
    "a = {x  for x in 'abracadabra' if x not in 'abc'}\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3.2集合运算\n",
    "\n",
    "<table><tr>\n",
    "<th>方法</th>\n",
    "<th>描述</th>\n",
    "</tr>\n",
    "<tr><td><a href=\"ref-set-add.html\" target=\"_blank\" rel=\"noopener noreferrer\">add()</a></td><td>为集合添加元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-clear.html\" target=\"_blank\" rel=\"noopener noreferrer\">clear()</a></td><td>移除集合中的所有元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-copy.html\" target=\"_blank\" rel=\"noopener noreferrer\">copy()</a></td><td>拷贝一个集合</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-difference.html\" target=\"_blank\" rel=\"noopener noreferrer\">difference()</a></td><td>返回多个集合的差集</td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-difference_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">difference_update()</a></td><td>移除集合中的元素，该元素在指定的集合也存在。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-discard.html\" target=\"_blank\" rel=\"noopener noreferrer\">discard()</a></td><td>删除集合中指定的元素</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-intersection.html\" target=\"_blank\" rel=\"noopener noreferrer\">intersection()</a></td><td>返回集合的交集</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-intersection_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">intersection_update()</a></td><td>\n",
    "  返回集合的交集。</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-isdisjoint.html\" target=\"_blank\" rel=\"noopener noreferrer\">isdisjoint()</a></td><td>判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False。</td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-issubset.html\" target=\"_blank\" rel=\"noopener noreferrer\">issubset()</a></td><td>判断指定集合是否为该方法参数集合的子集。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-issuperset.html\" target=\"_blank\" rel=\"noopener noreferrer\">issuperset()</a></td><td>判断该方法的参数集合是否为指定集合的子集</td></tr>\n",
    "<tr><td><a href=\"ref-set-pop.html\" target=\"_blank\" rel=\"noopener noreferrer\">pop()</a></td><td>随机移除元素</td></tr>\n",
    "<tr><td><a href=\"ref-set-remove.html\" target=\"_blank\" rel=\"noopener noreferrer\">remove()</a></td><td>移除指定元素</td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-symmetric_difference.html\" target=\"_blank\" rel=\"noopener noreferrer\">symmetric_difference()</a></td><td>返回两个集合中不重复的元素集合。</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-symmetric_difference_update.html\" target=\"_blank\" rel=\"noopener noreferrer\">symmetric_difference_update()</a></td><td>\n",
    "  移除当前集合中在另外一个指定集合相同的元素，并将另外一个指定集合中不同的元素插入到当前集合中。 </td></tr>\n",
    "  <tr>\n",
    "    <td><a href=\"ref-set-union.html\" target=\"_blank\" rel=\"noopener noreferrer\">union()</a></td><td>返回两个集合的并集</td>\n",
    "  </tr>\n",
    "<tr><td><a href=\"ref-set-update.html\" target=\"_blank\" rel=\"noopener noreferrer\">update()</a></td><td>给集合添加元素</td></tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pear', 'apple', 'Facebook', 'banana', 'orange'}\n",
      "{'F', 'pear', 'b', 'a', 'apple', 'Facebook', 'o', 'k', 'banana', 'e', 'orange', 'c'}\n",
      "{'F', 'pear', 'b', 'a', 'apple', 'Facebook', 'o', 'k', 'banana', 'e', 'orange', 'c'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'F'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 增加\n",
    "basket.add(\"Facebook\")\n",
    "print(basket)\n",
    "# 添加\n",
    "basket.update(\"Facebook\")\n",
    "print(basket)\n",
    "# 删除\n",
    "#basket.remove(\"apple\")\n",
    "print(basket)\n",
    "#basket.remove('none')  # throw error!!!\n",
    "basket.discard('none') # don't throw error!!!\n",
    "basket.pop()# throw one element random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pear'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算集合长度\n",
    "basket = {'apple','pear'}\n",
    "basket.pop()\n",
    "print(basket)\n",
    "len(basket)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#清理集合\n",
    "basket = {'apple','pear'}\n",
    "basket.clear()\n",
    "print(basket)\n",
    "len(basket)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=Section4></a>\n",
    "## 3.4 映射类型\n",
    "字典是另一种可变容器模型，且可存储任意类型对象。\n",
    "字典的每个键值 key=>value 对用冒号 : 分割，每个对之间用逗号(,)分割，整个字典包括在花括号 {} 中 ,格式如下所示：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 7.1.0 (20230121.1956)\n -->\n<!-- Title: 顺序结构 Pages: 1 -->\n<svg width=\"234pt\" height=\"188pt\"\n viewBox=\"0.00 0.00 234.00 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\n<title>顺序结构</title>\n<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-184 230,-184 230,4 -4,4\"/>\n<!-- 1 -->\n<g id=\"node1\" class=\"node\">\n<title>1</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"145,-180 91,-180 91,-144 145,-144 145,-180\"/>\n<text text-anchor=\"middle\" x=\"118\" y=\"-158.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">字典</text>\n</g>\n<!-- 2 -->\n<g id=\"node2\" class=\"node\">\n<title>2</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"70,-108 0,-108 0,-72 70,-72 70,-108\"/>\n<text text-anchor=\"middle\" x=\"35\" y=\"-86.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">name1：</text>\n</g>\n<!-- 1&#45;&gt;2 -->\n<g id=\"edge1\" class=\"edge\">\n<title>1&#45;&gt;2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M97.48,-143.7C87.41,-135.2 75.09,-124.81 64.07,-115.51\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"66.63,-113.09 56.73,-109.32 62.11,-118.45 66.63,-113.09\"/>\n</g>\n<!-- 3 -->\n<g id=\"node3\" class=\"node\">\n<title>3</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"148,-108 88,-108 88,-72 148,-72 148,-108\"/>\n<text text-anchor=\"middle\" x=\"118\" y=\"-86.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">key2：</text>\n</g>\n<!-- 1&#45;&gt;3 -->\n<g id=\"edge2\" class=\"edge\">\n<title>1&#45;&gt;3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M118,-143.7C118,-136.41 118,-127.73 118,-119.54\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"121.5,-119.62 118,-109.62 114.5,-119.62 121.5,-119.62\"/>\n</g>\n<!-- 4 -->\n<g id=\"node4\" class=\"node\">\n<title>4</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"226,-108 166,-108 166,-72 226,-72 226,-108\"/>\n<text text-anchor=\"middle\" x=\"196\" y=\"-86.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">key3：</text>\n</g>\n<!-- 1&#45;&gt;4 -->\n<g id=\"edge3\" class=\"edge\">\n<title>1&#45;&gt;4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M137.28,-143.7C146.66,-135.28 158.09,-125.02 168.38,-115.79\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"170.44,-118.64 175.54,-109.36 165.76,-113.43 170.44,-118.64\"/>\n</g>\n<!-- 5 -->\n<g id=\"node5\" class=\"node\">\n<title>5</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"62.5,-36 7.5,-36 7.5,0 62.5,0 62.5,-36\"/>\n<text text-anchor=\"middle\" x=\"35\" y=\"-14.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Value1</text>\n</g>\n<!-- 2&#45;&gt;5 -->\n<g id=\"edge4\" class=\"edge\">\n<title>2&#45;&gt;5</title>\n<path fill=\"none\" stroke=\"black\" d=\"M35,-71.7C35,-64.41 35,-55.73 35,-47.54\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"38.5,-47.62 35,-37.62 31.5,-47.62 38.5,-47.62\"/>\n</g>\n<!-- 6 -->\n<g id=\"node6\" class=\"node\">\n<title>6</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-36 90.5,-36 90.5,0 145.5,0 145.5,-36\"/>\n<text text-anchor=\"middle\" x=\"118\" y=\"-14.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Value2</text>\n</g>\n<!-- 3&#45;&gt;6 -->\n<g id=\"edge5\" class=\"edge\">\n<title>3&#45;&gt;6</title>\n<path fill=\"none\" stroke=\"black\" d=\"M118,-71.7C118,-64.41 118,-55.73 118,-47.54\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"121.5,-47.62 118,-37.62 114.5,-47.62 121.5,-47.62\"/>\n</g>\n<!-- 7 -->\n<g id=\"node7\" class=\"node\">\n<title>7</title>\n<polygon fill=\"none\" stroke=\"black\" points=\"223.5,-36 168.5,-36 168.5,0 223.5,0 223.5,-36\"/>\n<text text-anchor=\"middle\" x=\"196\" y=\"-14.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Value3</text>\n</g>\n<!-- 4&#45;&gt;7 -->\n<g id=\"edge6\" class=\"edge\">\n<title>4&#45;&gt;7</title>\n<path fill=\"none\" stroke=\"black\" d=\"M196,-71.7C196,-64.41 196,-55.73 196,-47.54\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"199.5,-47.62 196,-37.62 192.5,-47.62 199.5,-47.62\"/>\n</g>\n</g>\n</svg>\n",
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x1eb1b7965e0>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "import graphviz\n",
    "dot=graphviz.Digraph(comment='the round table',name=\"顺序结构\",node_attr={'shape': 'box'})\n",
    "dot.node('1','字典')\n",
    "dot.node('2','name1：')\n",
    "dot.node('3','key2：')\n",
    "dot.node('4','key3：')\n",
    "dot.node('5','Value1')\n",
    "dot.node('6','Value2')\n",
    "dot.node('7','Value3')\n",
    "dot.edges(['12','13','14','25','36','47'])\n",
    "dot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本操作\n",
    "键必须是唯一的，但值则不必。\n",
    "\n",
    "值可以取任何数据类型，但键必须是不可变的，如字符串，数字。\n",
    "\n",
    "一个简单的字典实例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'zhangsan', 'Age': 20, 'url': 'www.xauat.edu.cn'}\n"
     ]
    }
   ],
   "source": [
    "dict = {'name': 'zhangsan', 'Age': 20, 'url': 'www.xauat.edu.cn'}\n",
    "print(dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 访问字典里的值  \n",
    "把相应的键放入到方括号中，如下实例:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict['Age']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改字典   \n",
    "向字典添加新内容的方法是增加新的键/值对，修改或删除已有键/值对如下实例:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'ZHANGSAN', 'sex': '男'}\n"
     ]
    }
   ],
   "source": [
    "dict={'name':'ZHANGSAN','sex':'男'}\n",
    "print(dict)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数和方法\n",
    "<table><tr>\n",
    "<th>函数</th><th>返回值 ( 描述 )</th></tr>\n",
    "<tr><td><a> dict.keys() </a></td><td>返回所有键信息 </td></tr>\n",
    "<tr><td><a> dict.values() </a></td><td>返回所有值信息 </td></tr>\n",
    "<tr><td><a> dict.items() </a></td><td>返回所有键值对 </td></tr>\n",
    "<tr><td><a> dict.get('key',default) </a></td><td>键存在则返回相应值，否则返回默认值 </td></tr>\n",
    "<tr><td><a> dict.pop('key',default) </a></td><td> 键存在则返回相应值，否则删除删除键值对，返回默认值</td></tr>\n",
    "<tr><td><a> dict.popitem() </a></td><td>随机从字典中取出一个键值对 </td></tr>\n",
    "<tr><td><a> dict.clear() </a></td><td>删除所有的键值对 </td></tr>\n",
    "<tr><td><a> del dict['key'] </a></td><td>删除字典中某一个键值对 </td></tr>\n",
    "<tr><td><a> key in dict </a></td><td>如果键在字典中则返回True，否则返回False </td></tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])\n"
     ]
    }
   ],
   "source": [
    "\n",
    "car = {\n",
    "  'brand': 'Ford',\n",
    "  'model': 'Mustang',\n",
    "  'year': 1964\n",
    "}\n",
    "x = car.items()\n",
    "car['year'] = 2018\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['brand', 'model', 'year'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "x = True\n",
    "country_counter = {}\n",
    "\n",
    "def addone(country):\n",
    "    if country in country_counter:\n",
    "        country_counter[country] += 1\n",
    "    else:\n",
    "        country_counter[country] = 1\n",
    "\n",
    "addone('China')\n",
    "addone('Japan')\n",
    "addone('china')\n",
    "\n",
    "print(len(country_counter))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "b\n",
      "2\n",
      "2\n",
      "80, 100, 1000 最大值为:  1000\n",
      "-20, 100, 400最大值为:  400\n",
      "-80, -20, -10最大值为:  -10\n",
      "0, 100, -400最大值为: 100\n"
     ]
    }
   ],
   "source": [
    "print(max(1, 2))\n",
    "print(max('a', 'b'))\n",
    "print(max([1,2]))\n",
    "print(max((1,2)))\n",
    "print(\"80, 100, 1000 最大值为: \", max(80, 100, 1000))\n",
    "print(\"-20, 100, 400最大值为: \", max(-20, 100, 400))\n",
    "print(\"-80, -20, -10最大值为: \", max(-80, -20, -10))\n",
    "print(\"0, 100, -400最大值为:\", max(0, 100, -400))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制数为： 4567\n",
      "转换为二进制为： 0b1000111010111\n",
      "转换为八进制为： 0o10727\n",
      "转换为十六进制为： 0x11d7\n"
     ]
    }
   ],
   "source": [
    "dec = int(input(\"输入数字：\"))\n",
    " \n",
    "print(\"十进制数为：\", dec)\n",
    "print(\"转换为二进制为：\", bin(dec))\n",
    "print(\"转换为八进制为：\", oct(dec))\n",
    "print(\"转换为十六进制为：\", hex(dec))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正数\n"
     ]
    }
   ],
   "source": [
    "num = float(input(\"输入一个数字: \"))\n",
    "if num > 0:\n",
    "   print(\"正数\")\n",
    "elif num == 0:\n",
    "   print(\"零\")\n",
    "else:\n",
    "   print(\"负数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "67 和 45 的最大公约数为 1\n"
     ]
    }
   ],
   "source": [
    "def hcf(x, y):\n",
    "   \"\"\"该函数返回两个数的最大公约数\"\"\"\n",
    " \n",
    "   # 获取最小值\n",
    "   if x > y:\n",
    "       smaller = y\n",
    "   else:\n",
    "       smaller = x\n",
    " \n",
    "   for i in range(1,smaller + 1):\n",
    "       if((x % i == 0) and (y % i == 0)):\n",
    "           hcf = i\n",
    " \n",
    "   return hcf\n",
    " \n",
    " \n",
    "# 用户输入两个数字\n",
    "num1 = int(input(\"输入第一个数字: \"))\n",
    "num2 = int(input(\"输入第二个数字: \"))\n",
    " \n",
    "print( num1,\"和\", num2,\"的最大公约数为\", hcf(num1, num2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=\t\n",
      "1*2=\t2*2=\t\n",
      "1*3=\t2*3=\t3*3=\t\n",
      "1*4=\t2*4=\t3*4=\t4*4=\t\n",
      "1*5=\t2*5=\t3*5=\t4*5=\t5*5=\t\n",
      "1*6=\t2*6=\t3*6=\t4*6=\t5*6=\t6*6=\t\n",
      "1*7=\t2*7=\t3*7=\t4*7=\t5*7=\t6*7=\t7*7=\t\n",
      "1*8=\t2*8=\t3*8=\t4*8=\t5*8=\t6*8=\t7*8=\t8*8=\t\n",
      "1*9=\t2*9=\t3*9=\t4*9=\t5*9=\t6*9=\t7*9=\t8*9=\t9*9=\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print('{}*{}=\\t'.format(j,i,i*j),end='')\n",
    "    print ()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88.0 摄氏温度转为华氏温度为 190.4 \n"
     ]
    }
   ],
   "source": [
    "celsius = float(88)\n",
    "fahrenheit = (celsius * 1.8) + 32\n",
    "print('%0.1f 摄氏温度转为华氏温度为 %0.1f ' %(celsius,fahrenheit))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 87654.000 的平方根为 296.064\n"
     ]
    }
   ],
   "source": [
    "num = float(input('请输入一个数字： '))\n",
    "num_sqrt = num ** 0.5\n",
    "print(' %0.3f 的平方根为 %0.3f'%(num ,num_sqrt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "56 和 54 的最小公倍数为 1512\n"
     ]
    }
   ],
   "source": [
    "def lcm(x, y):\n",
    "   if x > y:\n",
    "       greater = x\n",
    "   else:\n",
    "       greater = y\n",
    " \n",
    "   while(True):\n",
    "       if((greater % x == 0) and (greater % y == 0)):\n",
    "           lcm = greater\n",
    "           break\n",
    "       greater += 1\n",
    " \n",
    "   return lcm\n",
    "num1 = int(input(\"输入第一个数字: \"))\n",
    "num2 = int(input(\"输入第二个数字: \"))\n",
    " \n",
    "print( num1,\"和\", num2,\"的最小公倍数为\", lcm(num1, num2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    August 2004\n",
      "Mo Tu We Th Fr Sa Su\n",
      "                   1\n",
      " 2  3  4  5  6  7  8\n",
      " 9 10 11 12 13 14 15\n",
      "16 17 18 19 20 21 22\n",
      "23 24 25 26 27 28 29\n",
      "30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "yy = int(input(\"输入年份: \"))\n",
    "mm = int(input(\"输入月份: \"))\n",
    "print(calendar.month(yy,mm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 30)\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "monthRange = calendar.monthrange(2016,9)\n",
    "print(monthRange)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-04-23\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "def getYesterday(): \n",
    "    today=datetime.date.today() \n",
    "    oneday=datetime.timedelta(days=1) \n",
    "    yesterday=today-oneday  \n",
    "    return yesterday\n",
    " \n",
    "# 输出\n",
    "print(getYesterday())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "斐波那契数列:\n",
      "0\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n",
      "89\n",
      "144\n",
      "233\n",
      "377\n",
      "610\n",
      "987\n",
      "1597\n",
      "2584\n",
      "4181\n",
      "6765\n",
      "10946\n",
      "17711\n",
      "28657\n",
      "46368\n",
      "75025\n",
      "121393\n",
      "196418\n",
      "317811\n",
      "514229\n",
      "832040\n",
      "1346269\n",
      "2178309\n",
      "3524578\n",
      "5702887\n",
      "9227465\n",
      "14930352\n",
      "24157817\n",
      "39088169\n",
      "63245986\n"
     ]
    }
   ],
   "source": [
    "def recur_fibo(n):\n",
    "   if n <= 1:\n",
    "       return n\n",
    "   else:\n",
    "       return(recur_fibo(n-1) + recur_fibo(n-2))\n",
    "nterms = int(input(\"您要输出几项? \"))\n",
    "if nterms <= 0:\n",
    "   print(\"输入正数\")\n",
    "else:\n",
    "   print(\"斐波那契数列:\")\n",
    "   for i in range(nterms):\n",
    "       print(recur_fibo(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_13084\\1394967771.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m     \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_13084\\1394967771.py\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m                 \u001b[0menough\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m                 \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0menough\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m             \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf'总共有{fish}条鱼'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    fish = 1\n",
    "    while True:\n",
    "        total, enough = fish, True\n",
    "        for _ in range(5):\n",
    "            if (total - 1) % 5 == 0:\n",
    "                total = (total - 1) \n",
    "            else:\n",
    "                enough = False\n",
    "                break\n",
    "        if enough:\n",
    "            print(f'总共有{fish}条鱼')\n",
    "            break\n",
    "        fish += 1\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按下回车开始计时，按下 Ctrl + C 停止计时。\n",
      "开始\n",
      "计时:  0.0 秒\n",
      "计时:  1.0 秒\n",
      "计时:  2.0 秒\n",
      "计时:  3.0 秒\n",
      "计时:  4.0 秒\n",
      "计时:  5.0 秒\n",
      "计时:  6.0 秒\n",
      "计时:  7.0 秒\n",
      "计时:  8.0 秒\n",
      "计时:  9.0 秒\n",
      "计时:  10.0 秒\n",
      "计时:  11.0 秒\n",
      "计时:  12.0 秒\n",
      "计时:  13.0 秒\n",
      "计时:  14.0 秒\n",
      "计时:  15.0 秒\n",
      "计时:  16.0 秒\n",
      "计时:  17.0 秒\n",
      "计时:  18.0 秒\n",
      "计时:  19.0 秒\n",
      "计时:  20.0 秒\n",
      "计时:  21.0 秒\n",
      "计时:  22.0 秒\n",
      "计时:  23.0 秒\n",
      "计时:  24.0 秒\n",
      "计时:  25.0 秒\n",
      "计时:  26.0 秒\n",
      "计时:  27.0 秒\n",
      "计时:  28.0 秒\n",
      "计时:  29.0 秒\n",
      "计时:  30.0 秒\n",
      "计时:  31.0 秒\n",
      "计时:  32.0 秒\n",
      "计时:  33.0 秒\n",
      "计时:  34.0 秒\n",
      "计时:  35.0 秒\n",
      "计时:  36.0 秒\n",
      "计时:  37.0 秒\n",
      "计时:  38.0 秒\n",
      "计时:  39.0 秒\n",
      "计时:  40.0 秒\n",
      "计时:  41.0 秒\n",
      "计时:  42.0 秒\n",
      "计时:  43.0 秒\n",
      "计时:  44.0 秒\n",
      "计时:  45.0 秒\n",
      "计时:  46.0 秒\n",
      "计时:  47.0 秒\n",
      "计时:  48.0 秒\n",
      "计时:  49.0 秒\n",
      "计时:  51.0 秒\n",
      "计时:  52.0 秒\n",
      "计时:  53.0 秒\n",
      "计时:  54.0 秒\n",
      "计时:  55.0 秒\n",
      "计时:  56.0 秒\n",
      "计时:  57.0 秒\n",
      "计时:  58.0 秒\n",
      "计时:  59.0 秒\n",
      "计时:  60.0 秒\n",
      "计时:  61.0 秒\n",
      "计时:  62.0 秒\n",
      "计时:  63.0 秒\n",
      "计时:  64.0 秒\n",
      "计时:  65.0 秒\n",
      "计时:  66.0 秒\n",
      "计时:  67.0 秒\n",
      "计时:  68.0 秒\n",
      "计时:  69.0 秒\n",
      "计时:  70.0 秒\n",
      "计时:  71.0 秒\n",
      "计时:  72.0 秒\n",
      "计时:  73.0 秒\n",
      "计时:  74.0 秒\n",
      "计时:  75.0 秒\n",
      "计时:  76.0 秒\n",
      "计时:  77.0 秒\n",
      "计时:  78.0 秒\n",
      "计时:  79.0 秒\n",
      "计时:  80.0 秒\n",
      "计时:  81.0 秒\n",
      "计时:  82.0 秒\n",
      "计时:  83.0 秒\n",
      "计时:  84.0 秒\n",
      "计时:  85.0 秒\n",
      "计时:  86.0 秒\n",
      "计时:  87.0 秒\n",
      "计时:  88.0 秒\n",
      "计时:  89.0 秒\n",
      "计时:  90.0 秒\n",
      "计时:  91.0 秒\n",
      "计时:  92.0 秒\n",
      "计时:  93.0 秒\n",
      "计时:  94.0 秒\n",
      "计时:  95.0 秒\n",
      "计时:  96.0 秒\n",
      "计时:  97.0 秒\n",
      "计时:  98.0 秒\n",
      "计时:  99.0 秒\n",
      "计时:  100.0 秒\n",
      "计时:  101.0 秒\n",
      "计时:  102.0 秒\n",
      "计时:  103.0 秒\n",
      "计时:  104.0 秒\n",
      "计时:  105.0 秒\n",
      "计时:  106.0 秒\n",
      "计时:  107.0 秒\n",
      "计时:  108.0 秒\n",
      "计时:  109.0 秒\n",
      "计时:  110.0 秒\n",
      "计时:  111.0 秒\n",
      "计时:  112.0 秒\n",
      "计时:  113.0 秒\n",
      "计时:  114.0 秒\n",
      "计时:  115.0 秒\n",
      "计时:  116.0 秒\n",
      "计时:  117.0 秒\n",
      "计时:  118.0 秒\n",
      "计时:  119.0 秒\n",
      "计时:  120.0 秒\n",
      "计时:  121.0 秒\n",
      "计时:  122.0 秒\n",
      "计时:  123.0 秒\n",
      "计时:  124.0 秒\n",
      "结束\n",
      "总共的时间为: 125.3 secs\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "  \n",
    "print('按下回车开始计时，按下 Ctrl + C 停止计时。')\n",
    "while True:\n",
    "    \n",
    "    input(\"\") # 如果是 python 2.x 版本请使用 raw_input() \n",
    "    starttime = time.time()\n",
    "    print('开始')\n",
    "    try:\n",
    "        while True:\n",
    "            print('计时: ', round(time.time() - starttime, 0), '秒')\n",
    "            time.sleep(1)\n",
    "    except KeyboardInterrupt:\n",
    "        print('结束')\n",
    "        endtime = time.time()\n",
    "        print('总共的时间为:', round(endtime - starttime, 2),'secs')\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "225\n"
     ]
    }
   ],
   "source": [
    "def sumOfSeries(n):\n",
    "    sum = 0\n",
    "    for i in range(1, n+1):\n",
    "        sum +=i*i*i\n",
    "         \n",
    "    return sum\n",
    "n = 5\n",
    "print(sumOfSeries(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组元素之和为 34\n"
     ]
    }
   ],
   "source": [
    "def _sum(arr,n):\n",
    "    return(sum(arr))\n",
    "arr=[]\n",
    "arr = [12, 3, 4, 15]\n",
    "n = len(arr)\n",
    "ans = _sum(arr,n)\n",
    "print ('数组元素之和为',ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 2, 8, 1]\n"
     ]
    }
   ],
   "source": [
    "def swapList(newList):\n",
    "    size = len(newList)\n",
    "     \n",
    "    temp = newList[0]\n",
    "    newList[0] = newList[size - 1]\n",
    "    newList[size - 1] = temp\n",
    "     \n",
    "    return newList\n",
    "\n",
    "newList = [1, 2,8, 3]\n",
    " \n",
    "print(swapList(newList))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 4 5 6 7 1 2 "
     ]
    }
   ],
   "source": [
    "def leftRotate(arr, d, n):\n",
    "    for i in range(d):\n",
    "        leftRotatebyOne(arr, n)\n",
    "def leftRotatebyOne(arr, n):\n",
    "    temp = arr[0]\n",
    "    for i in range(n-1):\n",
    "        arr[i] = arr[i+1]\n",
    "    arr[n-1] = temp\n",
    "         \n",
    " \n",
    "def printArray(arr,size):\n",
    "    for i in range(size):\n",
    "        print (\"%d\"% arr[i],end=\" \")\n",
    " \n",
    "\n",
    "arr = [1, 2, 3, 4, 5, 6, 7,8]\n",
    "leftRotate(arr, 2, 7)\n",
    "printArray(arr, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19, 65, 23, 90]\n"
     ]
    }
   ],
   "source": [
    "def swapPositions(list, pos1, pos2):\n",
    "     \n",
    "    list[pos1], list[pos2] = list[pos2], list[pos1]\n",
    "    return list\n",
    " \n",
    "List = [2333, 65, 19, 90]\n",
    "pos1, pos2  = 1, 3\n",
    " \n",
    "print(swapPositions(List, pos1-1, pos2-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19, 65, 23, 90]\n"
     ]
    }
   ],
   "source": [
    "def swapPositions(list, pos1, pos2):\n",
    "     \n",
    "    list[pos1], list[pos2] = list[pos2], list[pos1]\n",
    "    return list\n",
    " \n",
    "List = [23, 65, 19, 90]\n",
    "pos1, pos2  = 1, 3\n",
    " \n",
    "print(swapPositions(List, pos1-1, pos2-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "225\n"
     ]
    }
   ],
   "source": [
    "def sumOfSeries(n):\n",
    "    sum = 0\n",
    "    for i in range(1, n+1):\n",
    "        sum +=i*i*i\n",
    "         \n",
    "    return sum\n",
    "n = 5\n",
    "print(sumOfSeries(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看 4 是否在列表中 ( 使用循环 ) : \n",
      "存在\n",
      "查看 4 是否在列表中 ( 使用 in 关键字 ) : \n",
      "存在\n"
     ]
    }
   ],
   "source": [
    "test_list = [ 1, 63, 3, 5, 3, 4 ]\n",
    " \n",
    "print(\"查看 4 是否在列表中 ( 使用循环 ) : \")\n",
    " \n",
    "for i in test_list:\n",
    "    if(i == 4) :\n",
    "        print (\"存在\")\n",
    " \n",
    "print(\"查看 4 是否在列表中 ( 使用 in 关键字 ) : \")\n",
    "\n",
    "if (4 in test_list):\n",
    "    print (\"存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清空前: [6, 0, 4, 1]\n",
      "清空后: []\n"
     ]
    }
   ],
   "source": [
    "RUNOOB = [6, 0, 4, 1]\n",
    "print('清空前:', RUNOOB)  \n",
    " \n",
    "RUNOOB.clear()\n",
    "print('清空后:', RUNOOB)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 6, 19]\n"
     ]
    }
   ],
   "source": [
    "list_1 = [1, 2, 19, 4, 6]\n",
    "\n",
    "print(list(set(list_1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始列表: [4, 88, 2, 10, 15, 18]\n",
      "复制后列表: [4, 88, 2, 10, 15, 18]\n"
     ]
    }
   ],
   "source": [
    "def clone_runoob(li1):\n",
    "    li_copy = li1[:]\n",
    "    return li_copy\n",
    " \n",
    "li1 = [4, 88, 2, 10, 15, 18]\n",
    "li2 = clone_runoob(li1)\n",
    "print(\"原始列表:\", li1)\n",
    "print(\"复制后列表:\", li2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "def countX(lst, x):\n",
    "    count = 0\n",
    "    for ele in lst:\n",
    "        if (ele == x):\n",
    "            count = count + 1\n",
    "    return count\n",
    " \n",
    "lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]\n",
    "x = 8\n",
    "print(countX(lst, x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表元素之和为:  74\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    " \n",
    "list1 = [11, 89, 17, 18, 23]  \n",
    " \n",
    "for ele in range(0, len(list1)):\n",
    "    total = total + list1[ele]\n",
    " \n",
    "print(\"列表元素之和为: \", total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "225\n"
     ]
    }
   ],
   "source": [
    "def sumOfSeries(n):\n",
    "    sum = 0\n",
    "    for i in range(1, n+1):\n",
    "        sum +=i*i*i\n",
    "         \n",
    "    return sum\n",
    "n = 5\n",
    "print(sumOfSeries(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大元素为: 27760\n"
     ]
    }
   ],
   "source": [
    "list1 = [767, 27760, 4,785, 99]\n",
    " \n",
    "list1.sort()\n",
    " \n",
    "print(\"最大元素为:\", list1[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始字符串为 : Runoob\n",
      "字符串移除后为 : Ruoob\n"
     ]
    }
   ],
   "source": [
    "test_str = \"Runoob\"\n",
    "print (\"原始字符串为 : \" + test_str)\n",
    "new_str = \"\"\n",
    "for i in range(0, len(test_str)):\n",
    "    if i != 2:\n",
    "        new_str = new_str + test_str[i]\n",
    "\n",
    "print (\"字符串移除后为 : \" + new_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "存在！\n"
     ]
    }
   ],
   "source": [
    "def check(string, sub_str): \n",
    "    if (string.find(sub_str) == -1): \n",
    "        print(\"不存在！\") \n",
    "    else: \n",
    "        print(\"存在！\") \n",
    " \n",
    "string = \"www.runoob.com\"\n",
    "sub_str =\"runoob\"\n",
    "check(string, sub_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "def findLen(str): \n",
    "    counter = 0\n",
    "    while str[counter:]: \n",
    "        counter += 1\n",
    "    return counter \n",
    "  \n",
    "str = \"runob\"\n",
    "print(findLen(str))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('python310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a888cd810c90be07c3c921372a6d7ff433f5a38d5779dfb2a871d99db75673fb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
